package com.mf.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mf.dao.PictureDao;
import com.mf.entity.Picture;
import com.mf.entity.User;
import com.mf.util.DButil;
import com.mf.util.JdbcDaoHelper;

public class PictureDaoImpl extends BaseDaoImpl<Picture> implements PictureDao{

	
	
	
	private String sql;
	private Connection connection;
	private PreparedStatement pstat;
	ResultSet rs;
		
	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public PictureDaoImpl() {
		super();
		DButil util=new DButil();
		connection=util.getConnection();
	}

	@Override
	public List<Picture> SearchPicturesTypeByLike(String type) {
		List<Picture> list=new ArrayList<Picture>();
		this.setSql("select * from picture where type=?");
		Picture picture;
		try {
			pstat=JdbcDaoHelper.getPreparedStatement(sql);
			pstat.setString(1, type);
		    rs=pstat.executeQuery();
			while(rs.next()){
				picture=new Picture();
				picture.setId(rs.getInt("id"));
				picture.setTitle(rs.getString("title"));
				picture.setDetalis(rs.getString("detalis"));
				picture.setType(rs.getString("type"));
				picture.setBuildtime(rs.getString("buildtime"));
				picture.setBuilder(rs.getString("builder"));
				picture.setIntro(rs.getString("intro"));
				picture.setRealname(rs.getString("realname"));
				picture.setIsmusicpic(rs.getInt("ismusicpic"));
				list.add(picture);
				
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcDaoHelper.release(pstat, rs);
		}
		
		return null;
	}

	@Override
	public List<Picture> fuzzyQueryPictures(String nameLike) {
		List<Picture> list=new ArrayList<Picture>();
		this.setSql("select * from picture where title like ?");
		Picture picture;
		try {
			
			pstat=connection.prepareStatement(getSql());
			pstat.setString(1, '%'+nameLike+'%');
			//System.out.println(getSql());
			rs=pstat.executeQuery();
			while(rs.next()){
				picture=new Picture();
				picture.setId(rs.getInt("id"));
				picture.setTitle(rs.getString("title"));
				picture.setDetalis(rs.getString("detalis"));
				picture.setType(rs.getString("type"));
				picture.setBuildtime(rs.getString("buildtime"));
				picture.setBuilder(rs.getString("builder"));
				picture.setIntro(rs.getString("intro"));
				picture.setRealname(rs.getString("realname"));
				picture.setIsmusicpic(rs.getInt("ismusicpic"));
				list.add(picture);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcDaoHelper.release(pstat, rs);
		}
		return null;
	}

	@Override
	public List<Picture> AllPicture() {
		List<Picture> list=new ArrayList<Picture>();
		this.setSql("select * from picture ");
		Picture picture;
		try {
			//System.out.println(getSql());
			pstat=JdbcDaoHelper.getPreparedStatement(sql);
			rs=pstat.executeQuery();
			while(rs.next()){
				picture=new Picture();
				picture.setId(rs.getInt("id"));
				picture.setTitle(rs.getString("title"));
				picture.setDetalis(rs.getString("detalis"));
				picture.setType(rs.getString("type"));
				picture.setBuildtime(rs.getString("buildtime"));
				picture.setBuilder(rs.getString("builder"));
				picture.setIntro(rs.getString("intro"));
				picture.setRealname(rs.getString("realname"));
				picture.setIsmusicpic(rs.getInt("ismusicpic"));
				list.add(picture);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcDaoHelper.release(pstat, rs);
		}		
		return null;
	}

	@Override
	public Picture findPictureByRealname(String realname) {
		sql="SELECT * FROM realmf.picture where realname=? ;";
		Picture picture=new Picture();
		try {
			pstat=JdbcDaoHelper.getPreparedStatement(sql);
			pstat.setString(1, realname);
			rs=pstat.executeQuery();
			if (null!=rs) {
				while(rs.next()){
					picture.setId(rs.getInt("id"));
					picture.setTitle(rs.getString("title"));
					picture.setDetalis(rs.getString("detalis"));
					picture.setType(rs.getString("type"));
					picture.setBuildtime(rs.getString("buildtime"));
					picture.setBuilder(rs.getString("builder"));
					picture.setIntro(rs.getString("intro"));
					picture.setRealname(rs.getString("realname"));
					picture.setIsmusicpic(rs.getInt("ismusicpic"));
				}
				return picture;
			}

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcDaoHelper.release(pstat, rs);
			}
		
		return null;
	}
}